﻿using Company.Application.Share.Entities;
using Company.Hardware.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Company.Application.Share.Extensions
{
    public static class DeviceEntityExtension
    {
        public static Task Run(this DeviceEntity device)
        {
            return Task.Run(async() =>
            {
                var decoder = device.Decoder;
                if(decoder != null)
                {
                    await decoder.InitAsync(device.ConvertToConfig());
                    await decoder.StartAsync(device.Uploader);
                }
            });
        }

        public static Config ConvertToConfig(this DeviceEntity device)
        {
            var config = new Config();
            config.Name = device.Name;
            config.Protocol= device.Protocol;
            config.IpAddress= device.IpAddress;
            config.Port= device.Port;
            config.SerialName= device.SerialName;
            config.BaudRate= device.BaudRate;
            config.Status= device.Status;
            return config;
        }
    }
}
